//
//  TrainListTableViewController.swift
//  健身宝
//
//  Created by hzt on 2017/9/27.
//  Copyright © 2017年 hzt. All rights reserved.
//

import UIKit

class TrainListTableViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {
    let WIDTH = UIScreen.main.bounds.width
    let HEIGHT = UIScreen.main.bounds.height
    var trainlist:NSMutableArray!
    var tableView:UITableView!
    var mainScrollView:UIScrollView!
    override func viewDidLoad() {
        super.viewDidLoad()
        viewConfig()
        //viewWillAppear()
        DispatchQueue.global(qos: .default).async { self.getTrainList()  }
    }
    
    func viewConfig(){
        //view.backgroundColor = UIColor.white
        let scrollView = YScrollView(frame: CGRect(origin: CGPoint(x:0, y:64), size: CGSize(width: WIDTH, height: HEIGHT/4)))
        view.addSubview(scrollView)
        RONetworkMngTool.shared.fetchImage { (array) in
            var images:[String] = []
            var ids:[String] = []
            for obj in array{
                
                let dict = obj as! NSDictionary
                let str = dict.value(forKey: "thumb") as! String
                let pURL = "http://172.24.10.175/workout/Uploads/"
                let imgStr = pURL + str
                images.append(imgStr)
                
                let id = dict.value(forKey: "id") as! String
                ids.append(id)
            }
            
            scrollView.ids = ids
            scrollView.initialize(images: images)
            scrollView.pageControlInit(view: self.view)
        }
        
        tableView = UITableView(frame: CGRect(x: 0, y: 64+HEIGHT/4, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-69))
        tableView.register(TrainListCell.self, forCellReuseIdentifier: "TrainListCell")
        tableView.delegate = self
        tableView.dataSource = self
        //tableView.contentInsetAdjustmentBehavior = .never
        tableView.backgroundColor = view.backgroundColor
        tableView.tableFooterView = UIView()
        view.addSubview(tableView)
    }
    
    func getTrainList(){
        let urlStr = RO_GetTrainList
        let paraDict = ["sessionid":SessionID]
        AFHTTPRequestOperationManager().post(urlStr, parameters: paraDict, success: { (response:AFHTTPRequestOperation?, data:Any?) in
            let array = data as! NSArray
            //print(array)
            self.trainlist = array.mutableCopy() as! NSMutableArray
            //print(self.clubList)
            self.tableView.reloadData()
        }) { (response:AFHTTPRequestOperation?, error:Error?) in
            print(response!.debugDescription)
        }
        
    }

    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return trainlist?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TrainListCell", for: indexPath) as! TrainListCell
        let dict = trainlist![indexPath.row] as! NSDictionary
        let pURL = "http://172.24.10.175/workout/Uploads/"
        cell.imageView?.sd_setImage(with: URL(string: pURL+(dict.value(forKey: "thumb") as! String)), placeholderImage: UIImage(named:"placeholder"))
        cell.textLabel?.text = dict.value(forKey: "train_name") as? String
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let controller = TrainDetailController()
        controller.infoDict = trainlist![indexPath.row] as! NSDictionary
        //self.rdv_tabBarController.isTabBarHidden = true
        _ = navigationController?.pushViewController(controller, animated: true)
        //self.rdv_tabBarController.isTabBarHidden = false
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return (UIScreen.main.bounds.height-69)/3
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
//    func viewWillAppear() -> (ObjCBool){
//        super.viewWillAppear(false)
//        self.rdv_tabBarController.isTabBarHidden = false
//        return false
//    }
}
